import Vue from 'vue'
import Vuex from 'vuex'
import service from '../service.js'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		token: uni.getStorageSync('token'),
		invitation_code:uni.getStorageSync('invitation_code'),
		networkType: null,
		isConnected: null,
		SystemInfo: {},
		memberData: {},
		version: false,
		versionData: {},
		storeData: {},
		chatMsgNum: uni.getStorageSync('num') || 0, // 未读聊天信息数量
		is_chatView: false, // 聊天窗口是否打开中
		is_socket: false,	// 	是否链接socket
		memberID: '',	// 	用户id
		socketData: {},	//	socket 连接信息
		clearTimeoutObj:{}, //新增：保存订单自动取消的定时器
	},
	mutations: {
		login(state, provider) {
			state.token = provider;
			//缓存用户登陆状态
			uni.setStorageSync('token', provider);
		},
		logout(state) {
			state.token = null;
			state.invitation_code = null;
			uni.removeStorageSync('token')
			uni.removeStorageSync('invitation_code')
		},
		setMemberID(state, id){
			state.memberID = id;
		},
		setChatView(state, provider) {
			state.is_chatView = provider;
		},
		setChatMsgNum(state, num) {
			state.chatMsgNum = num;
			//缓存num
			uni.setStorageSync('num', num);
		},
		setSocket(state, provider) {
			state.is_socket = provider;
		},
		setCode(state, provider){
			state.invitation_code = provider;
			//缓存用户code
			uni.setStorageSync('invitation_code', provider);
		},
		setSocketData(state, data){
			state.socketData = data;
		},
		setStore(state,res) {
			state.storeData = res
		},
		versionTrue(state,item) {
			state.version = true
			state.versionData = item
		},
		getNetworkType(state) {			// 获取网络类型。
			uni.getNetworkType({
				success: function (res) {
					console.log(res.networkType);
					state.networkType = res.networkType
				}
			});
			// mutations 内调用另外一个方法
			this.commit('onNetworkStatusChange')
			this.commit('getSystemInfo')
		},
		onNetworkStatusChange(state) {		// 监听网络状态变化。
			uni.onNetworkStatusChange(function (res) {
				state.isConnected = res.isConnected
				state.networkType = res.networkType
			});
		},
		getSystemInfo(state) {				// 获取系统信息
			uni.getSystemInfo({
				success: function (res) {
					
					state.SystemInfo = res
				}
			});
		},
		vuexRequest(state,data){ // 公共网络请求
			service.request(data,data.success,data.fail,state.SystemInfo)
		},
		vuexUploadFile(state,data){	 // 上传图片
			service.uploadFile(data,data.success,data.fail,state.SystemInfo)
		},
	},
	actions: {
	
	}
})

export default store
